﻿Public Class ClsUserSettings
    Inherits ClsFile
    'represents user settings file

#Region "Attributes"
#Region "Private attributes"
    Private MoDsUserSettings As DsUserSettings = New DsUserSettings
#End Region
#End Region
#Region "Constructors"
    Sub New()
        MyBase.New()
    End Sub

    Sub New(ByVal IsPath As String)
        MyBase.New(IsPath)
    End Sub
#End Region
#Region "Properties"
#Region "Public attributes"
    Public Property DsUserSettings() As DsUserSettings
        Get
            Return MoDsUserSettings
        End Get
        Set(ByVal IoValue As DsUserSettings)
            MoDsUserSettings = IoValue
        End Set
    End Property
#End Region
#End Region
#Region "Functions"
#Region "Open file&DS, Save file&DS, Close file&DS"
    'open file
    Public Overrides Function OpenFile() As GeActionResult
        Try
            If MyBase.OpenFile = GeActionResult.kkOk Then
                If MoDsUserSettings.ReadXml(MsPath, XmlReadMode.Auto) Then
                    MeStateOfFile = GeFileState.kkOpened
                    Return GeActionResult.kkOk
                Else
                    'probably file is empty = without DS in XML file
                    CreateDefaultUserSettings()
                    Return GeActionResult.kkOk
                End If
            Else
                'file is not opened
                CreateDefaultUserSettings()
                Return GeActionResult.kkOk
            End If
        Catch ex As Exception
            Return GeActionResult.kkError
        End Try
    End Function
    'save file
    Public Function SaveFile() As GeActionResult
        Try
            MoDsUserSettings.WriteXml(MsPath, XmlWriteMode.WriteSchema)
        Catch ex As Exception
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Manipulating with DS"
#Region "INSERT"
    'insert new dock panels position to table UserSettings
    Private Function InsertNewDockPanelsPosition() As GeActionResult
        Try
            Dim LoRow As DataRow

            LoRow = MoDsUserSettings.Tables("UserSettings").NewRow()
            LoRow("DockPanelsPosition") = ""

            MoDsUserSettings.Tables("UserSettings").Rows.Add(LoRow)

            Return GeActionResult.kkOk
        Catch ex As Exception
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "UPDATE"
    'update dock panels position in table UserSettings
    Public Function UpdateDockPanelsPosition(ByVal IsValue As String) As GeActionResult
        Try
            Dim LoRows() As DataRow = MoDsUserSettings.Tables("UserSettings").Select()
            LoRows(0)("DockPanelsPosition") = IsValue

            Return GeActionResult.kkOk
        Catch ex As Exception
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "DELETE"

#End Region
#Region "SELECT"
    'gets dock panels position
    Public Function GetDockPanelsPosition() As String
        Try
            Dim LoRows() As DataRow = MoDsUserSettings.Tables("UserSettings").Select()
            Return LoRows(0).Item("DockPanelsPosition")
        Catch ex As Exception
            Debug.Print("CHYBA!!! - ClsUserSettings.GetDockPanelsPosition")
            Return "" 'default
        End Try
    End Function
#End Region
#End Region
#Region "Other Functions"
    'create default settings
    Private Function CreateDefaultUserSettings() As GeActionResult
        InsertNewDockPanelsPosition()

        Return GeActionResult.kkOk
    End Function
#End Region
#End Region

End Class
